package com.suixinsanren.design.state.demo.demo02;

public class WinnerState implements State {

    private final GumballMachine gumballMachine;

    public WinnerState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("请稍等，我们正在为您准备糖果");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("请稍等，我们正在为您准备糖果");
    }

    @Override
    public void turnCrank() {
        System.out.println("再次转动曲柄不能给您带来更多糖果");
    }

    @Override
    public void dispense() {
        gumballMachine.releaseBall();
        if (gumballMachine.getCount() == 0) {
            gumballMachine.setState(gumballMachine.getSoldOutState());
        } else {
            gumballMachine.releaseBall();
            System.out.println("你是赢家！你的硬币有两个口香糖");
            if (gumballMachine.getCount() > 0) {
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            } else {
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        }
    }

    @Override
    public String toString() {
        return "你的硬币可以获得两个口香糖，因为你是赢家！";
    }
}
